package document;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.Vector;

public class PostingList {
	private TreeMap<Integer, Posting> postings;
	
	public PostingList() {
		postings = new  TreeMap<Integer, Posting>();
	}

	public Vector<Integer> getAllDocumentIDs() {
		Vector<Integer> result = new Vector<Integer>();
		for (Posting posting : postings.values()) {
			result.add(posting.getDocID());
		}
		Collections.sort(result);
		return result;
	}

	public void addPosting(Integer docId, int wordPosition) {
		
		if(postings.containsKey(docId)) { //Document is already indexed
			postings.get(docId).addOccurence(wordPosition);
		}
		else { // Document is not indexed yet
			Posting posting = new Posting(docId);
			posting.addOccurence(wordPosition);
			postings.put(docId, posting);
		}
		
	}
	
	
	public Posting getPosting(int docId) {
		return postings.get(docId);
	}
	
	public Set<Map.Entry<Integer, Posting>> getPostings()
	{
		return postings.entrySet();
	}
}
